package test.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import test.util.Constant;


@Controller
@RequestMapping("/")
public class LoginController {
	@RequestMapping("login")
	public String login(HttpServletRequest request,HttpServletResponse response,HttpSession session) {
		Cookie[] cookies =  request.getCookies();
		String country="";
		if(cookies!=null) {
			for(Cookie coo:cookies) {
				if("country_id".equals(coo.getName())) {
					country=coo.getValue();
					break;
				}
				
			}
		}
		if("".equals(country)) {
			country=(String) session.getAttribute("country_id");
		}
		if("".equals(country)) {
			country=Constant.Coutry_Sets.CHN.getCountry_id();
		}
		Cookie coo=new Cookie("country_id",country);
		coo.setMaxAge(Constant.Int_Result.MaxAge.getAge());
		response.addCookie(coo);
		session.setAttribute("country_id", country);
		return "login";
	}
	@RequestMapping("toIndex")
	public String welcome() {
		return "toIndex";
	}
	@RequestMapping("logout")
	public String logout() {
		return "login";
	}

}
